package com.app.gynews5;


import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import static android.R.id.content;
import static com.app.gynews5.R.id.title;

public class MainActivity extends AppCompatActivity {

    private EditText addmsgTitle;
    private EditText addmsgContent;
    private Button submitRtn;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }
    private void initView(){
        addmsgTitle = (EditText) findViewById(R.id.add_messages_title_edt);
        addmsgContent = (EditText) findViewById(R.id.add_messages_content_edt);
        submitRtn = (Button) findViewById(R.id.submit_btn);
        submitRtn.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                initData(addmsgTitle.getText().toString(),addmsgContent.getText().toString());
            }
        });


    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initData(String title,String content){
        String url = "http://10.1.63.142:8090/freshmenapp/messages/addone";
        JSONObject jsonObject = new JSONObject();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = format.format(new Date());
        try{
            jsonObject.put("title",title);
            jsonObject.put("content",content);
            jsonObject.put("name","张三");
            jsonObject.put("date",data);

        }catch (JSONException e){
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(MainActivity.this,jsonObject.optString("msg")+"",Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);


    }
}
